/*
 * Score.java
 *
 * Created on 2006.12.13, PM 9:03
 *
 * Manage high scores
 */

package Puyo;
import java.io.*;
import javax.microedition.lcdui.*; //for Graphics
/**
 * One score entry
 */
class ScoreEntry{
    public String playerName;
    public int    score;
    public int    level; //this is reserved for future use
    public void   set(String sPlayerName, int nScore, int nLevel){
        playerName = sPlayerName;
        score      = nScore;
        level      = nLevel;
    }
    public void   set(ScoreEntry aEntry){
        playerName = aEntry.playerName;
        score      = aEntry.score;
        level      = aEntry.level;
    }
}

/**
 *
 * @author Ming Li
 */
public class Scores {
    static final int MAX_SCORE_ENTRIES = 6;
    
    private ScoreEntry scoreEntries[]; 
    private Font smallBoldItalicFont;
    private Font smallFont;
    
    /** Creates a new instance of Score */
    public Scores() {
        scoreEntries = new ScoreEntry[MAX_SCORE_ENTRIES];
        for (int i = 0; i < MAX_SCORE_ENTRIES; i++) {
          scoreEntries[i] = new ScoreEntry();  
          scoreEntries[i].set("SuperGamer",0,0);
        }
        smallBoldItalicFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD | Font.STYLE_ITALIC, Font.SIZE_SMALL);
        smallFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    }
    
    //draw high scores
    public void draw(Graphics g, int screenWidth){
        g.setFont(smallBoldItalicFont);
        g.drawString( "Player", 3, 3, Graphics.TOP|Graphics.LEFT );
        g.drawString( "Score", screenWidth/2, 3, Graphics.TOP|Graphics.LEFT );
        
        int nFontHeight = smallFont.getHeight() + 1 ;
        g.setFont(smallFont);
        for( int i=0; i<Scores.MAX_SCORE_ENTRIES; i++ ) {    
          g.drawString( scoreEntries[i].playerName, 3, i * nFontHeight + 25, Graphics.TOP|Graphics.LEFT );
          g.drawString( ""+scoreEntries[i].score, screenWidth/2, i * nFontHeight + 25, Graphics.TOP|Graphics.LEFT );
        }
        g.setFont(Font.getDefaultFont());
    }
    
    /** if the given score high enough to be listed in the top score list ? */
    public boolean isScoreHighEnough( int score ) {
      if( scoreEntries[MAX_SCORE_ENTRIES - 1].score < score )
        return true;
      return false;
    }
    
    public void resetScores() {
       for (int i = 0; i < MAX_SCORE_ENTRIES; i++) {
        scoreEntries[i].set("SuperGamer",0,0);
       }
       writeScores();
    }
    
    public void insertHighscore( String name, int score, byte level ) {
        int insertHere =0;
        for( int i=0; i < MAX_SCORE_ENTRIES; i++ ) {
          if( score>scoreEntries[i].score ) {
            insertHere = i;
            break;
          }
        }
        for( int n= MAX_SCORE_ENTRIES - 1; n > insertHere; n-- ) {
          scoreEntries[n].set( scoreEntries[n-1]);
        }
        scoreEntries[insertHere].set( name, score, level );
        writeScores();
    }
      
    public void writeScores() {
        PuyoIO.write("HighScore",scoresToByteArray());
    }
    
    public void readScores() {
        byte[] data = PuyoIO.read("HighScore");
        if(data != null)            
            byteArrayToScores(data);
    }
    
    private byte[] scoresToByteArray(){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(baos);
        // stream
        try {
          for (int i = 0; i < MAX_SCORE_ENTRIES; i++) {
            os.writeUTF(scoreEntries[i].playerName);
            os.writeInt(scoreEntries[i].score);
            os.writeInt(scoreEntries[i].level);            
          }
          os.close();
        } catch( IOException ex ) {
          System.out.println( ex.toString() );
        }
        return baos.toByteArray();
    }
    
    private void byteArrayToScores(byte[] data){
        DataInputStream is = new DataInputStream( new ByteArrayInputStream(data) );
        try {
          for (int i = 0; i < MAX_SCORE_ENTRIES; i++) {
            scoreEntries[i].playerName = is.readUTF();
            scoreEntries[i].score = is.readInt();           
            scoreEntries[i].level = is.readInt();
          }
          is.close();
        } catch( IOException ex ) {
          System.out.println(ex.toString() );
        }
    }
}
